package com.example.g7640.util;

import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class BaseConvertor {

    public static <S,D> D convert(S source,Class<D> clazz){
        try {
            D d = clazz.newInstance();
            BeanUtils.copyProperties(source,d);
            return d;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    public static <S,D> List<D> convertList(List<S> sourceList,Class<D> clazz){
        if(CollectionUtils.isEmpty(sourceList)){
            return Collections.emptyList();
        }
        List<D> descList = new ArrayList<>(sourceList.size());
        for(S source : sourceList){
            descList.add(convert(source,clazz));
        }
        return descList;
    }
}
